package com.hxs.hxsserver.controller.user;

import com.hxs.hxsserver.config.redis.RedisService;
import com.hxs.hxsserver.config.redis.UserKey;
import com.hxs.hxsserver.controller.BaseController;
import com.hxs.hxsserver.domain.dto.JsonResult;
import com.hxs.hxsserver.domain.dto.user.PasswordLoginDTO;
import com.hxs.hxsserver.domain.vo.UserLoginVO;
import com.hxs.hxsserver.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("login")
@Api(value = "登录退出模块接口")
public class LoginController extends BaseController {

    @Resource
    private UserService userService;
    @Resource
    private RedisService redisService;

    @ApiOperation(value = "用户密码登录", response = UserLoginVO.class)
    @PostMapping("/passwordLogin")
    public JsonResult<UserLoginVO> passwordLogin(@RequestBody @Validated PasswordLoginDTO dto) {
        UserLoginVO userLoginVO = userService.passwordLogin(dto);
        return JsonResult.success(userLoginVO);
    }


    @ApiOperation(value = "登出", response = Boolean.class)
    @PostMapping("/logout")
    public JsonResult<Boolean> logout() {
        redisService.delete(UserKey.userToken,request.getHeader("Authorization"));
        return JsonResult.success(true);
    }

    @ApiOperation(value = "获取验证码", response = String.class)
    @GetMapping("/getPicCode")
    public JsonResult<Object> getPicCode() {
        return JsonResult.success(userService.getPicCode());
    }

}
